package com.education.service.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author 余一
 * @date 2021/9/1  10:36
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private SystemWebSocketHandler webSocketHandler;
    @Autowired
    private HandshakeInterceptor handshakeInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
            webSocketHandlerRegistry.addHandler(webSocketHandler,"/webSocket")
                    .addInterceptors(handshakeInterceptor)
                    .setAllowedOrigins("*"); //配置允许跨域

            webSocketHandlerRegistry.addHandler(webSocketHandler,"/sockJs/webSocket")
                    .addInterceptors(handshakeInterceptor)
                    .setAllowedOrigins("*").withSockJS();//配置允许跨域
    }
}
